<?php
include_once("bean/option.class.php");
include_once("includes/util.php");
include_once("bean/country.class.php");

/**
 * Option class. Process for all function of Option tab in admin page.
 */
class options 
{
	var $db;
 	var $smarty;
 	
    function options($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * Save or Update option's information
	 */
	function updateOption()
	{
		$arr = array("code" => -1, 
				"message" => _("Options are not updated."));
		
		$res = option::saveOption($this->db, 
				"page_title", $_POST["page_title"]);
		
		if($res == true)
		{
			$arr['code'] = 0;
			$arr['message'] = _("Option has been updated.");
		}
		
		echo array2json($arr);
	}
	
	/**
	 * Save or Update country language mapping
	 */
	function updateLang()
	{
		$arr = array("code" => -1, 
				"message" => _("Country is not updated."));
		
		$langlist = @implode("," , $_POST["ID"]);
		
		$res = country::updateLanguage($this->db, 
				$_POST['country'], $langlist);
				
		if($res == true)
		{
			$arr['code'] = 0;
			$arr['message'] = _("Country has been updated.<br />");
		}
		
		echo array2json($arr);
	}
	
	function getLangList($id)
	{
		global $langmeta;
		
		$langs = country::getLanguageList($this->db, $id);
					
		$ListLang = array();
		
			foreach($langmeta as $thelang)
			{
				if($langs != null)
				{
					if(in_array($thelang['META']['LANG_ID'], $langs))
					{
						$thelang['isSel'] = 1;
					}
					else
					{
						$thelang['isSel'] = 0;
					}
				}
				
				$ListLang[] = $thelang;
			}
	
		$this->smarty->assign("LANGMETA", $ListLang);
	}
	
	function addCountry()
	{
		$country_code = trim($_POST['country_code']);
		$country_name = trim($_POST['country_name']);
		
		$res = array("code" => "-1", "message" => _("Could not add country."));
		
		if($country_code != "" && $country_name != "")
		{
			if(country::getCountryById($this->db, $_POST['country_code']) == null)
			{
				$newrow = country::addCountry($this->db, $_POST);
				
				if($newrow == 0)
				{
					$res = array("code" => "0", "message" => _("Country has been added."));
					$res = array_merge($res, $_POST);
				}
			}
			else
			{
				$res = array("code" => "-1", "message" => _("Country code is duplicated."));
			}
		}
		
		echo array2json($res);
	}
	
	function deleteCountry()
	{
		$arr = array("code" => -1, 
				"message" => _("Could not delete country."));
		
		//$res = country::deleteCountry($this->db, $_POST['country']);
			$res = true;
		if($res == true)
		{
			$arr['code'] = 0;
			$arr['message'] = _("Country has been deleted.<br />");
		}
		
		echo array2json($arr);
	}
	
	/**
	 * This function will be call be index.php base on mod specified by GET param.
	 */
	function process()
	{
		if(isset($_POST['act']))
		{
			if($_POST['act'] == 'request_lang')
			{
				$id = $_POST['id'];
				if($id != "")
				{
					$this->getLangList($id);
					
					$output = $this->smarty->fetch("admin/options_lang_list.tpl");
					
					echo $output;
				}
			}
			else if($_POST['act'] == 'update')
			{
				$this->updateOption();
			}
			else if($_POST['act'] == 'updatelang')
			{
				$this->updateLang();
			}
			else if($_POST['act'] == 'deletecountry')
			{
				$this->deleteCountry();
			}
			else if($_POST['act'] == 'addcountry')
			{
				$this->addCountry();
			}
		}
		else
		{
			$opt = option::loadAllOption($this->db);
			
			if($opt != '') {
			  foreach($opt as $item)
			  {
				  $this->smarty->assign($item->option_key, $item->option_value);
			  }
			}
	
			$countries = country::getAllCountry($this->db);
			
			//$this->getLangList($countries[0]->country_code);
			
			$this->smarty->assign("COUNTRIES", $countries);
			$this->smarty->display('options.tpl');
			$this->smarty->clear_assign("LANGMETA");
		}
	}
}
?>
